const db = require('../db/index')

// 获取公告信息列表
exports.noticeList = (req, res) => {
  const sql = `select * from  notice`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    if (results.length < 0) return res.cc('获取公告信息列表失败！')
    res.send({
      status: 0,
      message: '获取公告信息列表成功！',
      data: results
    })
  })
}

// 发布公告
exports.insertNoticeList = (req, res) => {
  const sql = `insert into notice set title = ?,content = ?`
  db.query(sql, [req.body.title, req.body.content], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('发布公告失败！')
    res.send({
      status: 0,
      message: '发布公告成功！'
    })
  })
}

// 根据No删除信息
exports.deleteByNid = (req, res) => {
  const sql = `delete from notice where nid = ? `
  db.query(sql, req.body.nid, (err, results) => {
    // console.log(req.body.No)
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('删除失败！')
    res.cc('删除成功！', 0)
  })
}

// 修改公告
exports.updateNotice = (req, res) => {
  const sql = `update notice set title = ?,content = ? where nid = ? `
  db.query(sql, [req.body.title, req.body.content, req.body.nid], (err, results) => {
    // console.log(req.body.No)
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('更新公告失败！')
    res.cc('更新公告成功！', 0)
  })
}
